//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "VanillaVPPOption.h"
using namespace Cephei::QL::Experimental::Finitedifferences;
#include <gen/QL/Instruments/SwingExercise.h>
#include <gen/QL/Payoff.h>
#include <gen/QL/Exercise.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instruments/MultiAssetOption.h>
using namespace Cephei::QL::Instruments;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Finitedifferences::CVanillaVPPOption::CVanillaVPPOption (Double heatRate, Double pMin, Double pMax, UInt64 tMinUp, UInt64 tMinDown, Double startUpFuel, Double startUpFixCost, UInt64 initialState, Cephei::QL::Instruments::ISwingExercise^ exercise, Cephei::QL::IPricingEngine^ QL_Pricer) : CMultiAssetOption(CVanillaVPPOption::typeid)
{
    CSwingExercise^ _Cexercise;
    try
    {
#ifdef HANDLE
        _phVanillaVPPOption = NULL;
#endif
        QuantLib::Real _heatRate = (QuantLib::Real)ValueHelper::Convert (heatRate); //d
        QuantLib::Real _pMin = (QuantLib::Real)ValueHelper::Convert (pMin); //d
        QuantLib::Real _pMax = (QuantLib::Real)ValueHelper::Convert (pMax); //d
        QuantLib::Size _tMinUp = (QuantLib::Size)ValueHelper::Convert (tMinUp); //d
        QuantLib::Size _tMinDown = (QuantLib::Size)ValueHelper::Convert (tMinDown); //d
        QuantLib::Real _startUpFuel = (QuantLib::Real)ValueHelper::Convert (startUpFuel); //d
        QuantLib::Real _startUpFixCost = (QuantLib::Real)ValueHelper::Convert (startUpFixCost); //d
        QuantLib::Size _initialState = (QuantLib::Size)ValueHelper::Convert (initialState); //d
        _Cexercise = safe_cast<CSwingExercise^> (exercise);
        _Cexercise->Lock();
        boost::shared_ptr<QuantLib::SwingExercise>& _exercise = static_cast<boost::shared_ptr<QuantLib::SwingExercise>&> (_Cexercise->GetShared ()); 
        _ppVanillaVPPOption = new boost::shared_ptr<QuantLib::VanillaVPPOption> (new QuantLib::VanillaVPPOption ( _heatRate,  _pMin,  _pMax,  _tMinUp,  _tMinDown,  _startUpFuel,  _startUpFixCost,  _initialState,  _exercise ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppVanillaVPPOption)->setPricingEngine (_QL_Pricer);
        SetMultiAssetOption (boost::dynamic_pointer_cast<QuantLib::MultiAssetOption> (*_ppVanillaVPPOption));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cexercise != nullptr) _Cexercise->Unlock();
    }
}
Cephei::QL::Experimental::Finitedifferences::CVanillaVPPOption::CVanillaVPPOption (boost::shared_ptr<QuantLib::VanillaVPPOption>& childNative, Object^ owner) : CMultiAssetOption(CVanillaVPPOption::typeid)
{
#ifdef HANDLE
	_phVanillaVPPOption = NULL;
#endif
	_ppVanillaVPPOption = &childNative;
    _ppMultiAssetOption = new boost::shared_ptr<QuantLib::MultiAssetOption> (boost::dynamic_pointer_cast<QuantLib::MultiAssetOption> (*_ppVanillaVPPOption));
}
Cephei::QL::Experimental::Finitedifferences::CVanillaVPPOption::CVanillaVPPOption (QuantLib::VanillaVPPOption& childNative, Object^ owner) : CMultiAssetOption(CVanillaVPPOption::typeid)
{
#ifdef HANDLE
	_phVanillaVPPOption = NULL;
#endif
	_ppVanillaVPPOption = new boost::shared_ptr<QuantLib::VanillaVPPOption> (&childNative);
    _ppMultiAssetOption = new boost::shared_ptr<QuantLib::MultiAssetOption> (boost::dynamic_pointer_cast<QuantLib::MultiAssetOption> (*_ppVanillaVPPOption));
    _VanillaVPPOptionOwner = owner;
    _MultiAssetOptionOwner = owner;
}

Cephei::QL::Experimental::Finitedifferences::CVanillaVPPOption::CVanillaVPPOption (CVanillaVPPOption^ copy) : CMultiAssetOption(CVanillaVPPOption::typeid)
{
#ifdef HANDLE
	_phVanillaVPPOption = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppVanillaVPPOption = new boost::shared_ptr<QuantLib::VanillaVPPOption> (copy->GetShared());
        _ppMultiAssetOption = new boost::shared_ptr<QuantLib::MultiAssetOption> (boost::dynamic_pointer_cast<QuantLib::MultiAssetOption> (*_ppVanillaVPPOption));
    }
}
Cephei::QL::Experimental::Finitedifferences::CVanillaVPPOption::CVanillaVPPOption (PLATFORM::Type^ t) : CMultiAssetOption(CVanillaVPPOption::typeid)
{
#ifdef HANDLE
	_phVanillaVPPOption = NULL;
#endif
	if (!t->IsSubclassOf(CVanillaVPPOption::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Finitedifferences::CVanillaVPPOption::CVanillaVPPOption (QuantLib::Handle<QuantLib::VanillaVPPOption>& childNative, Object^ owner)  : CMultiAssetOption(CVanillaVPPOption::typeid)
{
	_phVanillaVPPOption = &childNative;
	_ppVanillaVPPOption = &static_cast<boost::shared_ptr<QuantLib::VanillaVPPOption>>(childNative.currentLink());
    _ppMultiAssetOption = new boost::shared_ptr<QuantLib::MultiAssetOption> (boost::dynamic_pointer_cast<QuantLib::MultiAssetOption> (*_ppVanillaVPPOption));
    _VanillaVPPOptionOwner = owner;
}
Cephei::QL::Experimental::Finitedifferences::CVanillaVPPOption::CVanillaVPPOption (QuantLib::Handle<QuantLib::VanillaVPPOption> childNative)  : CMultiAssetOption(CVanillaVPPOption::typeid)
{
	_phVanillaVPPOption = &childNative;
	_ppVanillaVPPOption = &static_cast<boost::shared_ptr<QuantLib::VanillaVPPOption>>(childNative.currentLink());
    _ppMultiAssetOption = new boost::shared_ptr<QuantLib::MultiAssetOption> (boost::dynamic_pointer_cast<QuantLib::MultiAssetOption> (*_ppVanillaVPPOption));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Finitedifferences::CVanillaVPPOption::CVanillaVPPOption (QuantLib::VanillaVPPOption childNative)  : CMultiAssetOption(CVanillaVPPOption::typeid)
{
#ifdef HANDLE
	_phVanillaVPPOption = NULL;
#endif
	_ppVanillaVPPOption = new boost::shared_ptr<QuantLib::VanillaVPPOption> (new QuantLib::VanillaVPPOption (childNative));
    _ppMultiAssetOption = new boost::shared_ptr<QuantLib::MultiAssetOption> (boost::dynamic_pointer_cast<QuantLib::MultiAssetOption> (*_ppVanillaVPPOption));
}
#endif

Cephei::QL::Experimental::Finitedifferences::CVanillaVPPOption::~CVanillaVPPOption ()
{
    if (_ppVanillaVPPOption != NULL)
    {
	    delete _ppVanillaVPPOption;
        _ppVanillaVPPOption = NULL;
    }
}
Cephei::QL::Experimental::Finitedifferences::CVanillaVPPOption::!CVanillaVPPOption ()
{
    if (_ppVanillaVPPOption != NULL)
    {
	    delete _ppVanillaVPPOption;
    }
}
QuantLib::VanillaVPPOption& Cephei::QL::Experimental::Finitedifferences::CVanillaVPPOption::GetReference ()
{
    if (_ppVanillaVPPOption == NULL) throw REFNEW NativeNullException ();
	return **_ppVanillaVPPOption;
}
boost::shared_ptr<QuantLib::VanillaVPPOption>& Cephei::QL::Experimental::Finitedifferences::CVanillaVPPOption::GetShared ()
{
    if (_ppVanillaVPPOption == NULL) throw REFNEW NativeNullException ();
	return *_ppVanillaVPPOption;
}
QuantLib::VanillaVPPOption* Cephei::QL::Experimental::Finitedifferences::CVanillaVPPOption::GetPointer ()
{
    if (_ppVanillaVPPOption == NULL) throw REFNEW NativeNullException ();
	return &**_ppVanillaVPPOption;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::VanillaVPPOption>& Cephei::QL::Experimental::Finitedifferences::CVanillaVPPOption::GetHandle ()
{
	if (_phVanillaVPPOption == NULL)
	{
		_phVanillaVPPOption = new Handle<QuantLib::VanillaVPPOption> (*_ppVanillaVPPOption);
	}
	return *_phVanillaVPPOption;
}
#endif
bool Cephei::QL::Experimental::Finitedifferences::CVanillaVPPOption::HasNative () 
{
	return (_ppVanillaVPPOption != NULL);
}

Boolean Cephei::QL::Experimental::Finitedifferences::CVanillaVPPOption::IsExpired::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppVanillaVPPOption)->isExpired ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Finitedifferences::IVanillaVPPOption^ Cephei::QL::Experimental::Finitedifferences::CVanillaVPPOption_Factory::Create (Double heatRate, Double pMin, Double pMax, UInt64 tMinUp, UInt64 tMinDown, Double startUpFuel, Double startUpFixCost, UInt64 initialState, Cephei::QL::Instruments::ISwingExercise^ exercise, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return REFNEW CVanillaVPPOption ( heatRate,  pMin,  pMax,  tMinUp,  tMinDown,  startUpFuel,  startUpFixCost,  initialState,  exercise,  QL_Pricer);
}
